package com.wanpinghui.supplier.widgets;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wanpinghui.supplier.R;
import com.yunshl.yunshllibrary.view.YunShlEditText;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2017/12/25
 ***/

public class PlusMinuesView extends LinearLayout {
    private int count;
    private int lastCount;
    private TextView textViewPlus;
    private TextView textViewMinues;
    private YunShlEditText editText;

    private OnCountChangeListener onCountChangeListener;

    public void setOnCountChangeListener(OnCountChangeListener listener) {
        this.onCountChangeListener = listener;
    }


    public PlusMinuesView(Context context) {
        super(context);
        init();
    }

    public PlusMinuesView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PlusMinuesView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.view_plus_minues, this);
        textViewMinues = (TextView) findViewById(R.id.tv_minues);
        textViewPlus = (TextView) findViewById(R.id.tv_plus);
        editText = (YunShlEditText) findViewById(R.id.edt_count);
        textViewPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count++;
                editText.setText(Integer.toString(count));
                if (onCountChangeListener != null) {
                    onCountChangeListener.onPlus();
                }
            }
        });
        textViewMinues.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (count > 0) {
                    count--;
                    editText.setText(Integer.toString(count));
                    if (onCountChangeListener != null) {
                        onCountChangeListener.onMinues();
                    }
                }
            }
        });
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    count = Integer.parseInt(s.toString());
                    if (count < 0) {
                        editText.setText("0");
                    }
                }
                if (count <= 0) {
                    textViewMinues.setVisibility(GONE);
                    editText.setVisibility(GONE);
                } else {
                    textViewMinues.setVisibility(VISIBLE);
                    editText.setVisibility(VISIBLE);
                }
                if (onCountChangeListener != null && lastCount != count) {
                    onCountChangeListener.onCountChange(count);
                }
                lastCount = count;
            }
        });
    }

    public void setCount(int count) {
        this.count = count;
        if (count > 0) {
            editText.setText(Integer.toString(count));
        } else {
            editText.setText("");
        }
    }

    public interface OnCountChangeListener {
        void onPlus();

        void onMinues();

        void onCountChange(int count);
    }
}
